<template>
  <div class="dept-lists">
    <el-card class="!border-none" shadow="never">
      <el-form
        ref="formRef"
        class="mb-[-16px]"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="大厦编号" prop="sn">
          <el-input
            class="w-[280px]"
            v-model="queryParams.sn"
            clearable
            @keyup.enter="getLists"
            placeholder="请输入大厦编号"
          />
        </el-form-item>
        <el-form-item label="大厦名称" prop="name">
          <el-input
            class="w-[280px]"
            v-model="queryParams.name"
            clearable
            @keyup.enter="getLists"
            placeholder="请输入大厦名称"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getLists">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div class="mb-4">
        <el-button type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新建
        </el-button>
      </div>
      <el-table size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="大厦编号" prop="sn" min-width="120" />
        <el-table-column label="大厦名称" prop="name" min-width="100" />
        <el-table-column label="大厦总楼层" prop="totalFloor" min-width="100" />
        <el-table-column label="详细地址" prop="address" min-width="120" />
        <el-table-column
          label="预计送达时间"
          prop="deliveryTime"
          min-width="120"
        />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button
              v-if="row.pid !== 0"
              type="danger"
              link
              @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>
<script lang="ts" setup name="department">
import type { ElTable, FormInstance } from "element-plus";
import { usePaging } from "@/hooks/usePaging";
import EditPopup from "./edit.vue";
import { getMansionList, createMansionSn, delMansion } from "@/api/admin/index";
import feedback from "@/utils/feedback";
const tableRef = shallowRef<InstanceType<typeof ElTable>>();
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const formRef = shallowRef<FormInstance>();
let isExpand = false;
const loading = ref(false);
const lists = ref<any[]>([]);
const queryParams = reactive({
  sn: "",
  name: "",
});
const showEdit = ref(false);

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: getMansionList,
  params: queryParams,
});
onActivated(() => {
  getLists();
});

getLists();

/* const getLists = async () => {
  // loading.value = true;
  const res: any = await getMansionList(queryParams);
  lists.value = res.lists;
  console.log("🚀 ~ file: index.vue:140 ~ lists.value :", lists.value);
  // loading.value = false;
};

const resetParams = () => {
  formRef.value?.resetFields();
  getLists();
}; */

const handleAdd = async (id?: number) => {
  showEdit.value = true;
  const sn = await createMansionSn();
  await nextTick();
  if (sn) {
    editRef.value?.setFormData({
      sn,
    });
  }
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.getDetail(data);
};

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await delMansion({ id });
  feedback.msgSuccess("删除成功");
  getLists();
};

const handleExpand = () => {
  isExpand = !isExpand;
  toggleExpand(lists.value, isExpand);
};

const toggleExpand = (children: any[], unfold = true) => {
  for (const key in children) {
    tableRef.value?.toggleRowExpansion(children[key], unfold);
    if (children[key].children) {
      toggleExpand(children[key].children!, unfold);
    }
  }
};

onMounted(async () => {
  //   await getLists();
  nextTick(() => {
    handleExpand();
  });
});
</script>
